<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. Struts2下载解压后的目录结构如下：从一个高水平角度看，Struts2 是一个MVC拉动的（或MVC2）框架，Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的：  操作（Actions） 拦截器（Interceptors） 值栈（Value Stack）/OGNL 结果（Result）/结果类型 视图技术">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="JakartaEE-Struts2使用">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Struts2使用/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. Struts2下载解压后的目录结构如下：从一个高水平角度看，Struts2 是一个MVC拉动的（或MVC2）框架，Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的：  操作（Actions） 拦截器（Interceptors） 值栈（Value Stack）/OGNL 结果（Result）/结果类型 视图技术">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c9eea53a36647d2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9497c2b4b1f0a59f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-dd21dae9be5562e4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f2cd576adcbb2baf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-861435401c37b58c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-aaf68388f954497b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d3ce295849a1c112.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-92bb8e0c7d570707.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8776eb0f24240319.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-7a909b5d3ec830b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0a8bcb1bf76bfa33.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:51:03.937Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JakartaEE-Struts2使用">
<meta name="twitter:description" content="####1. Struts2下载解压后的目录结构如下：从一个高水平角度看，Struts2 是一个MVC拉动的（或MVC2）框架，Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的：  操作（Actions） 拦截器（Interceptors） 值栈（Value Stack）/OGNL 结果（Result）/结果类型 视图技术">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-c9eea53a36647d2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Struts2使用/"/>





  <title>JakartaEE-Struts2使用 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Struts2使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JakartaEE-Struts2使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. <a href="http://struts.apache.org/" target="_blank" rel="noopener">Struts2下载</a><br>解压后的目录结构如下：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-c9eea53a36647d2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br>从一个高水平角度看，Struts2 是一个MVC拉动的（或MVC2）框架，Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的：</p>
<ul>
<li>操作（Actions）</li>
<li>拦截器（Interceptors）</li>
<li>值栈（Value Stack）/OGNL</li>
<li>结果（Result）/结果类型</li>
<li>视图技术<a id="more"></a>
</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-9497c2b4b1f0a59f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p><strong>请求生命周期</strong><br>通过上述图片的描述，我们可以依照下面几点解释在Struts2 中用户的请求生命周期：</p>
<ul>
<li>用户发送一个资源需求的请求到服务器（例如：页面）。</li>
<li>核心控制器查看请求后确定适当的动作。</li>
<li>使用验证、文件上传等配置拦截器功能。</li>
<li>执行选择的动作来完成请求的操作。</li>
<li>如果需要的话，配置的拦截器可做任何后期处理。</li>
<li>最后，由视图显示结果并返回给用户。</li>
</ul>
<p>####2. HelloWorld程序</p>
<p>######1.  四个组件：</p>
<ul>
<li>Action（操作）<br>创建一个动作类，包含完整的业务逻辑并控制用户、模型以及视图间的交互。</li>
<li>Interceptors（拦截器）<br>这是控制器的一部分，可依据需求创建拦截器，或使用现有的拦截器。</li>
<li>View（视图）<br>创建一个JSP与用户进行交互，获取输入并呈现最终信息。</li>
<li>Configuration Files（配置文件）<br>创建配置文件来连接动作、视图以及控制器，这些文件分别是struts.xml、web.xml以及struts.properties。</li>
</ul>
<p>######2. 创建项目<br><img src="https://upload-images.jianshu.io/upload_images/3110861-dd21dae9be5562e4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"><br>记得勾选Generate web.xml deployment descriptor.<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f2cd576adcbb2baf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"><br>目录结构：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-861435401c37b58c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>######3. 导入jar包<br>将struts-2.3.34文件中的lib文件夹中的一下jar包引入项目的WebContent/WEB-INF/lib文件夹中</p>
<ul>
<li>commons-fileupload-1.3.2.jar</li>
<li>commons-io-2.2.jar</li>
<li>commons-lang-2.4.jar</li>
<li>commons-lang3-3.2.jar</li>
<li>commons-logging-1.1.3.jar</li>
<li>freemarker-2.3.22.jar</li>
<li>javassist-3.11.0.GA</li>
<li>ognl-3.0.21.jar</li>
<li>struts2-core-2.3.34.jar</li>
<li>xwork-core.2.3.34.jar</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-aaf68388f954497b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p>######4. 创建HelloWorldAction类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public class HelloWorldAction &#123;</span><br><span class="line">	private String name;</span><br><span class="line">	</span><br><span class="line">	public String execute() throws Exception&#123;</span><br><span class="line">		return &quot;success&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String getName() &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public void setName(String name) &#123;</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######5. 创建HelloWorld.jsp页面<br>在/Webcontent/下创建jsp文件夹，将所有创建的jsp页面存储在这个文件夹，在/Webcontent/jsp/文件夹下创建HelloWorld.jsp页面<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;s&quot; uri=&quot;/struts-tags&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt;    </span><br><span class="line">		&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;Insert title here&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		Hello World, &lt;s:property value=&quot;name&quot;/&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>######6. 在/WebContent/jsp/页面创建index.jsp文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;s&quot; uri=&quot;/struts-tags&quot; %&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt;    </span><br><span class="line">		&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;Insert title here&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;h1&gt;Hello World From Struts2&lt;/h1&gt;</span><br><span class="line">		&lt;form action=&quot;hello&quot;&gt;</span><br><span class="line">			&lt;label for=&quot;name&quot;&gt; Please enter your name&lt;/label&gt;&lt;br&gt;</span><br><span class="line">			&lt;input type=&quot;text&quot; name=&quot;name&quot;/&gt;</span><br><span class="line">			&lt;input type=&quot;submit&quot; value=&quot;Say Hello&quot;/&gt;</span><br><span class="line">		&lt;/form&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>######7. 在src目录下创建struts.xml文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE struts PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span><br><span class="line">	 &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span><br><span class="line">&lt;struts&gt;</span><br><span class="line">	&lt;constant name=&quot;struts.devMode&quot; value=&quot;true&quot; /&gt;</span><br><span class="line">	&lt;package name=&quot;helloworld&quot; extends=&quot;struts-default&quot;&gt;</span><br><span class="line">		&lt;action name=&quot;hello&quot;</span><br><span class="line">			class=&quot;com.mazaiting.struct2.HelloWorldAction&quot;</span><br><span class="line">			method=&quot;execute&quot;&gt;</span><br><span class="line">			&lt;result name=&quot;success&quot;&gt;/jsp/HelloWorld.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">	&lt;/package&gt;</span><br><span class="line">&lt;/struts&gt;</span><br></pre></td></tr></table></figure></p>
<p>######8. 在src目录下创建logging.properties配置文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">org.apache.catalina.core.ContainerBase.[Catalina].level = INFO</span><br><span class="line">org.apache.catalina.core.ContainerBase.[Catalina].handlers = \ java.util.logging.ConsoleHandler</span><br></pre></td></tr></table></figure></p>
<p>######9. 在web.xml文件中进行配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;3.0&quot;&gt;</span><br><span class="line">  &lt;display-name&gt;HelloWorld&lt;/display-name&gt;</span><br><span class="line">  &lt;welcome-file-list&gt;</span><br><span class="line">    &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;</span><br><span class="line">    &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;</span><br><span class="line">    &lt;welcome-file&gt;/jsp/index.jsp&lt;/welcome-file&gt;</span><br><span class="line">    &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;</span><br><span class="line">    &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;</span><br><span class="line">    &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;</span><br><span class="line">  &lt;/welcome-file-list&gt;</span><br><span class="line">  &lt;filter&gt;</span><br><span class="line">  	&lt;filter-name&gt;struts2&lt;/filter-name&gt;</span><br><span class="line">  	&lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;</span><br><span class="line">  &lt;/filter&gt;</span><br><span class="line">  </span><br><span class="line">  &lt;filter-mapping&gt;</span><br><span class="line">  	&lt;filter-name&gt;struts2&lt;/filter-name&gt;</span><br><span class="line">  	&lt;url-pattern&gt;/*&lt;/url-pattern&gt;</span><br><span class="line">  &lt;/filter-mapping&gt;</span><br><span class="line">&lt;/web-app&gt;</span><br></pre></td></tr></table></figure></p>
<p>######10. 运行项目<br><img src="https://upload-images.jianshu.io/upload_images/3110861-d3ce295849a1c112.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>在输入框中输入文字，点击Say Hello<br><img src="https://upload-images.jianshu.io/upload_images/3110861-92bb8e0c7d570707.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>######11. 设置索引<br>修改struts.xml文件为<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE struts PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span><br><span class="line">	 &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span><br><span class="line">&lt;struts&gt;</span><br><span class="line">	&lt;constant name=&quot;struts.devMode&quot; value=&quot;true&quot; /&gt;</span><br><span class="line">	&lt;package name=&quot;helloworld&quot; extends=&quot;struts-default&quot;&gt;</span><br><span class="line">		&lt;action name=&quot;index&quot;&gt;</span><br><span class="line">			&lt;result&gt;/jsp/index.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">		&lt;action name=&quot;hello&quot;</span><br><span class="line">			class=&quot;com.mazaiting.struct2.HelloWorldAction&quot;</span><br><span class="line">			method=&quot;execute&quot;&gt;</span><br><span class="line">			&lt;result name=&quot;success&quot;&gt;/jsp/HelloWorld.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">	&lt;/package&gt;</span><br><span class="line">&lt;/struts&gt;</span><br></pre></td></tr></table></figure></p>
<p>在浏览器输入<code>http://localhost:8080/HelloWorld/index.action</code>即可访问.</p>
<p>####3. 配置文件</p>
<p>######1). struts.xml文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE struts PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span><br><span class="line">	 &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span><br><span class="line">&lt;struts&gt;</span><br><span class="line">	&lt;constant name=&quot;struts.devMode&quot; value=&quot;true&quot; /&gt;</span><br><span class="line">	&lt;package name=&quot;helloworld&quot; extends=&quot;struts-default&quot;&gt;</span><br><span class="line">		&lt;action name=&quot;index&quot;&gt;</span><br><span class="line">			&lt;result&gt;/jsp/index.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">		&lt;action name=&quot;hello&quot;</span><br><span class="line">			class=&quot;com.mazaiting.struct2.HelloWorldAction&quot;</span><br><span class="line">			method=&quot;execute&quot;&gt;</span><br><span class="line">			&lt;result name=&quot;success&quot;&gt;/jsp/HelloWorld.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">		&lt;!-- more actions can be listed here --&gt;</span><br><span class="line">	&lt;/package&gt;</span><br><span class="line">	&lt;!-- more packages can be listed here --&gt;</span><br><span class="line">&lt;/struts&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li><package>标签具有以下属性：</package></li>
</ul>
<table>
<thead>
<tr>
<th></th>
<th>属性</th>
<th>描述 </th>
</tr>
</thead>
<tbody>
<tr>
<td>name（必需）</td>
<td>为package的唯一标识</td>
</tr>
<tr>
<td>extends</td>
<td>指定package继承另一package的所有配置。通常情况下，我们使用struts-default作为package的基础。</td>
</tr>
<tr>
<td>abstract</td>
<td>定义package为抽象的。如果标记为true，则package不能被最终用户使用。</td>
</tr>
<tr>
<td>namespace</td>
<td>Actions的唯一命名空间</td>
</tr>
</tbody>
</table>
<ul>
<li><p><constant>标签以及name和value属性将用于覆盖default.properties中定义的任一属性，就像我们设置的struts.devMode属性一样。设置struts.devMode属性允许我们在日志文件中查看更多的调试消息。</constant></p>
</li>
<li><p>Results（结果）确定在执行操作后返回到浏览器的内容，而从操作返回的字符串应该是结果的名称。 Results按上述方式配置，或作为“全局”结果配置，可用于包中的每个操作。 Results有<code>name</code>和<code>type</code>属性可选，默认的name值是“success”。</p>
</li>
<li><p>Struts.xml文件可以随着时间的推移而增长，因此通过包打破它是使它模块化的一种方式，但struts提供了另一种模块化struts.xml文件的方法，你可以将文件拆分为多个xml文件，并用以下方式导入它们。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE struts PUBLIC</span><br><span class="line">    &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span><br><span class="line">    &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span><br><span class="line">&lt;struts&gt;</span><br><span class="line">     &lt;include file=&quot;my-struts1.xml&quot;/&gt;</span><br><span class="line">     &lt;include file=&quot;my-struts2.xml&quot;/&gt;</span><br><span class="line">&lt;/struts&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######2). struts.properties文件<br>配置文件提供了一种机制来改变框架的默认行为。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">### When set to true, Struts will act much more friendly for developers</span><br><span class="line">struts.devMode = true</span><br><span class="line"></span><br><span class="line">### Enables reloading of internationalization files</span><br><span class="line">struts.i18n.reload = true</span><br><span class="line"></span><br><span class="line">### Enables reloading of XML configuration files</span><br><span class="line">struts.configuration.xml.reload = true</span><br><span class="line"></span><br><span class="line">### Sets the port that the server is run on</span><br><span class="line">struts.url.http.port = 8080</span><br></pre></td></tr></table></figure></p>
<p>####4. 拦截器</p>
<p>######1). 分类</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>拦截器和说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>alias 允许参数在请求之间使用不同的别名。</td>
</tr>
<tr>
<td>2</td>
<td>checkbox 通过为未检查的复选框添加参数值false，以辅助管理复选框。</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>conversionError 将字符串转换为参数类型的错误信息放置到action的错误字段中。</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>createSession 自动创建HTTP会话（如果尚不存在）。</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>debugging 为开发人员提供一些不同的调试屏幕。</td>
<td></td>
</tr>
<tr>
<td>6</td>
<td>execAndWait 当action在后台执行时，将用户发送到中间的等待页面。</td>
<td></td>
</tr>
<tr>
<td>7</td>
<td>exception 映射从action到结果抛出的异常，允许通过重定向自动处理异常。</td>
<td></td>
</tr>
<tr>
<td>8</td>
<td>fileUpload 便于文件上传。</td>
<td></td>
</tr>
<tr>
<td>9</td>
<td>i18n 在用户会话期间跟踪选定的区域。</td>
<td></td>
</tr>
<tr>
<td>10</td>
<td>logger 通过输出正在执行的action的名称提供简单的日志记录。</td>
<td></td>
</tr>
<tr>
<td>11</td>
<td>params 设置action上的请求参数。</td>
<td></td>
</tr>
<tr>
<td>12</td>
<td>prepare 这通常用于执行预处理工作，例如设置数据库连接。</td>
<td></td>
</tr>
<tr>
<td>13</td>
<td>profile 允许记录action的简单分析信息。</td>
<td></td>
</tr>
<tr>
<td>14</td>
<td>scope 在会话或应用程序范围内存储和检索action的状态。</td>
<td></td>
</tr>
<tr>
<td>15</td>
<td>ServletConfig 提供可访问各种基于servlet信息的action。</td>
<td></td>
</tr>
<tr>
<td>16</td>
<td>timer 以action执行时间的形式提供简单的分析信息。</td>
<td></td>
</tr>
<tr>
<td>17</td>
<td>token 检查action的有效性，以防止重复提交表单。</td>
<td></td>
</tr>
<tr>
<td>18</td>
<td>validation 提供action的验证支持。</td>
<td></td>
</tr>
</tbody>
</table>
<p>######2). 使用Struts2中的拦截器<br>I. 给HelloWorldAction 添加timer拦截器(测量执行action方法所需的时间)及params拦截器(将请求参数发送给action)<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 根标记元素 --&gt;</span><br><span class="line">&lt;struts&gt;</span><br><span class="line">	&lt;!-- 开发模式-默认为false --&gt;</span><br><span class="line">	&lt;constant name=&quot;struts.devMode&quot; value=&quot;true&quot; /&gt;</span><br><span class="line">	&lt;!-- 声明不同的包, 标签允许配置的分离和模块化 --&gt;</span><br><span class="line">	&lt;package name=&quot;helloworld&quot; extends=&quot;struts-default&quot;&gt;</span><br><span class="line">		&lt;action name=&quot;index&quot;&gt;</span><br><span class="line">			&lt;result&gt;/jsp/index.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">		&lt;action name=&quot;hello&quot;</span><br><span class="line">			class=&quot;com.mazaiting.struct2.HelloWorldAction&quot;</span><br><span class="line">			method=&quot;execute&quot;&gt;</span><br><span class="line">			&lt;!-- 设置action上的请求参数 --&gt;</span><br><span class="line">			&lt;interceptor-ref name=&quot;params&quot;/&gt;</span><br><span class="line">			&lt;!-- 以action执行时间的形式提供简单的分析信息  --&gt;</span><br><span class="line">			&lt;interceptor-ref name=&quot;timer&quot;/&gt;</span><br><span class="line">			&lt;result name=&quot;success&quot;&gt;/jsp/HelloWorld.jsp&lt;/result&gt;</span><br><span class="line">		&lt;/action&gt;</span><br><span class="line">		&lt;!-- more actions can be listed here --&gt;</span><br><span class="line">	&lt;/package&gt;</span><br><span class="line">	&lt;!-- more packages can be listed here --&gt;</span><br><span class="line">&lt;/struts&gt;</span><br></pre></td></tr></table></figure></p>
<p>II. 测试<br><img src="https://upload-images.jianshu.io/upload_images/3110861-8776eb0f24240319.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>在最后两行打印了当前action执行的时间。</p>
<p>######3). 自定义拦截器<br>I. 创建拦截器类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public class MyInterceptor extends AbstractInterceptor &#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String intercept(ActionInvocation invocation) throws Exception &#123;</span><br><span class="line">		System.out.println(&quot;Pre-Processing--先执行&quot;);</span><br><span class="line">		</span><br><span class="line">		// 执行方法</span><br><span class="line">		String result = invocation.invoke();</span><br><span class="line">		</span><br><span class="line">		System.out.println(&quot;Post-Processing--后执行&quot;);</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">``` </span><br><span class="line">![图9.png](https://upload-images.jianshu.io/upload_images/3110861-8f0479c4bfc7dfa6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</span><br><span class="line"></span><br><span class="line">II. 创建Action类</span><br></pre></td></tr></table></figure></p>
<p>public class HelloWorldAction extends ActionSupport{<br>    private String name;</p>
<pre><code>public String execute() throws Exception{
    System.out.println(&quot;execute...&quot;);
    return &quot;success&quot;;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}    
</code></pre><p>}<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">III. structs.xml文件中配置</span><br></pre></td></tr></table></figure></p>
<p>&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;<br>&lt;!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”<br>     “<a href="http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;" target="_blank" rel="noopener">http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</a><br><!-- 根标记元素 --></p>
<p><struts><br>    <!-- 开发模式-默认为false --><br>    <constant name="struts.devMode" value="true"><br>    <!-- 声明不同的包, 标签允许配置的分离和模块化 --><br>    <package name="helloworld" extends="struts-default"><br>        <interceptors><br>            <interceptor name="myInterceptor" class="com.mazaiting.struct2.MyInterceptor"><br>        </interceptor></interceptors><br>        <action name="index"><br>            <result>/jsp/index.jsp</result><br>        </action><br>        <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"><br>            <!-- 设置action上的请求参数 --><br>            <interceptor-ref name="params"><br>            <!-- 以action执行时间的形式提供简单的分析信息  --><br>            <interceptor-ref name="timer"><br>            <!-- 自定义拦截器 --><br>            <interceptor-ref name="myInterceptor"><br>            <result name="success">/jsp/HelloWorld.jsp</result><br>        </interceptor-ref></interceptor-ref></interceptor-ref></action><br>        <!-- more actions can be listed here --><br>    </package><br>    <!-- more packages can be listed here --><br></constant></struts><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">IV. 执行结果</span><br><span class="line">![图10.png](https://upload-images.jianshu.io/upload_images/3110861-f3e30343747fcfde.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</span><br><span class="line"></span><br><span class="line">####5. 文件上传</span><br><span class="line">**注意：服务器可能有适当的安全策略，禁止你写入临时目录以外的目录以及属于Web应用程序的目录。使用FileUpload过滤器**</span><br><span class="line">######1). 创建jsp文件</span><br><span class="line">I. 文件上传fileupload.jsp文件</span><br></pre></td></tr></table></figure></p>
<p>&lt;%@ page language=”java” contentType=”text/html; charset=UTF-8”<br>    pageEncoding=”UTF-8”%&gt;<br>&lt;%@ taglib prefix=”s” uri=”/struts-tags”%&gt;<br>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “<a href="http://www.w3.org/TR/html4/loose.dtd&quot;&gt;" target="_blank" rel="noopener">http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</a></p>
<p><html><br>    <head><br>        <meta http-equiv="pragma" content="no-cache"><br>        <meta http-equiv="cache-control" content="no-cache"><br>        <meta http-equiv="expires" content="0"><br>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br>        <meta http-equiv="description" content="This is my page"><br>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>        <title>File Upload</title><br>    </head><br>    <body><br>        <form action="upload" method="post" enctype="multipart/form-data"><br>            <label for="myFile">Upload your file</label><br>            <input type="file" name="myFile"><br>            <input type="submit" value="upload"><br>        </form><br>    </body><br></html><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">II. 创建上传成功success.jsp文件</span><br></pre></td></tr></table></figure></p>
<p>&lt;%@ page language=”java” contentType=”text/html; charset=UTF-8”<br>    pageEncoding=”UTF-8”%&gt;<br>&lt;%@ taglib prefix=”s” uri=”/struts-tags” %&gt;<br>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “<a href="http://www.w3.org/TR/html4/loose.dtd&quot;&gt;" target="_blank" rel="noopener">http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</a></p>
<p><html><br>    <head><br>        <meta http-equiv="pragma" content="no-cache"><br>        <meta http-equiv="cache-control" content="no-cache"><br>        <meta http-equiv="expires" content="0"><br>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br>        <meta http-equiv="description" content="This is my page"><br>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>        <title>File Upload Success</title><br>    </head><br>    <body><br>        You have successfully uploaded &lt;s:property value=”myFileFileName”/&gt;<br>    </body><br></html><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">III. 创建上传失败error.jsp文件</span><br></pre></td></tr></table></figure></p>
<p>&lt;%@ page language=”java” contentType=”text/html; charset=UTF-8”<br>    pageEncoding=”UTF-8”%&gt;<br>&lt;%@ taglib prefix=”s” uri=”/struts-tags” %&gt;<br>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “<a href="http://www.w3.org/TR/html4/loose.dtd&quot;&gt;" target="_blank" rel="noopener">http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</a></p>
<p><html><br>    <head><br>        <meta http-equiv="pragma" content="no-cache"><br>        <meta http-equiv="cache-control" content="no-cache"><br>        <meta http-equiv="expires" content="0"><br>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br>        <meta http-equiv="description" content="This is my page"><br>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>        <title>File Upload Error</title><br>    </head><br>    <body><br>        There has been an error in uploading the file.<br>    </body><br></html><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">######2). 创建FileUploadAction</span><br></pre></td></tr></table></figure></p>
<p>/**</p>
<ul>
<li>默认情况下，FileUpload拦截器为你提供三个参数，它们分别按以下方式命名：</li>
<li>[文件名参数] - 这是用户已上传的实际文件。在这个例子中它将是“myFile”</li>
<li>[文件名参数]ContentType - 这是上传的文件的内容类型。在这个例子中，它将是“myFileContentType”</li>
<li>[文件名参数]FileName - 这是上传的文件的名称。在这个例子中，它将是“myFileFileName”</li>
<li><p>@author mazaiting<br>*/<br>public class FileUploadAction extends ActionSupport{</p>
<p> private File myFile;<br> private String myFileContentType;<br> private String myFileFileName;<br> private String destPath;</p>
<p> @Override<br> public String execute() throws Exception {</p>
<pre><code>destPath = &quot;E:/file/&quot;;
</code></pre></li>
</ul>
<pre><code>    try {
        System.out.println(&quot;Src File name: &quot; + myFile);
        System.out.println(&quot;Dst File name: &quot; + myFileFileName);

        File destFile = new File(destPath, myFileFileName);
        FileUtils.copyFile(myFile, destFile);
    } catch (Exception e) {
        e.printStackTrace();
        return ERROR;
    }

    return SUCCESS;
}

public File getMyFile() {
    return myFile;
}

public void setMyFile(File myFile) {
    this.myFile = myFile;
}

public String getMyFileContentType() {
    return myFileContentType;
}

public void setMyFileContentType(String myFileContentType) {
    this.myFileContentType = myFileContentType;
}

public String getMyFileFileName() {
    return myFileFileName;
}

public void setMyFileFileName(String myFileFileName) {
    this.myFileFileName = myFileFileName;
}

public String getDestPath() {
    return destPath;
}

public void setDestPath(String destPath) {
    this.destPath = destPath;
}
</code></pre><p>}<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">######3). 配置文件</span><br><span class="line"></span><br><span class="line">序号	|属性和说明|</span><br><span class="line">--- | --- |</span><br><span class="line">1	| struts.multipart.maxSize 可接受的上传文件的最大值（以字节为单位），默认值为250M。|</span><br><span class="line">2	| struts.multipart.parser 用于上传多部分表单的库，默认为jakarta。</span><br><span class="line">3	| struts.multipart.saveDir 存储临时文件的位置，默认是javax.servlet.context.tempdir。</span><br></pre></td></tr></table></figure></p>
<p>&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;<br>&lt;!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”<br>     “<a href="http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;" target="_blank" rel="noopener">http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</a><br><!-- 根标记元素 --></p>
<p><struts><br>    <!-- 开发模式-默认为false --><br>    <constant name="struts.devMode" value="true"><br>    <!-- 可接受的上传文件的最大值（以字节为单位），默认值为250M --><br>    <constant name="struts.multipart.maxSize" value="1000000"><br>    <!-- 声明不同的包, 标签允许配置的分离和模块化 --><br>    <package name="helloworld" extends="struts-default"><br>        <interceptors><br>            <interceptor name="myInterceptor" class="com.mazaiting.struct2.MyInterceptor"><br>        </interceptor></interceptors><br>        <action name="index"><br>            <result>/jsp/index.jsp</result><br>        </action><br>        <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"><br>            <!-- 设置action上的请求参数 --><br>            <interceptor-ref name="params"><br>            <!-- 以action执行时间的形式提供简单的分析信息  --><br>            <interceptor-ref name="timer"><br>            <!-- 自定义拦截器 --><br>            <interceptor-ref name="myInterceptor"><br>            <result name="success">/jsp/HelloWorld.jsp</result><br>        </interceptor-ref></interceptor-ref></interceptor-ref></action><br>        <!-- more actions can be listed here --><br>        <action name="upload" class="com.mazaiting.struct2.FileUploadAction"><br>            <!-- 注意过滤器的顺序 --><br>            <interceptor-ref name="fileUpload"><br>                <param name="maximumSize">500000000<br>                <param name="allowedTypes">image/jpeg,image/gif,text/plain,application/vnd.ms-powerpoint<br>                <param name="allowedExtensions">.txt,.ppt,.jpeg,.jpg,.gif<br>            </interceptor-ref><br>            <interceptor-ref name="basicStack"><br>            <result name="success">/jsp/success.jsp</result><br>            <result name="error">/jsp/error.jsp</result><br>        </interceptor-ref></action><br>    </package><br>    <!-- more packages can be listed here --><br></constant></constant></struts><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">**fileUpload拦截器有两个参数：maximumSize和allowedTypes。**</span><br><span class="line">- maximumSize参数是设置所允许的文件大小的最大值（默认约为2MB）。</span><br><span class="line">- allowedTypes参数是所允许的内容（MIME）类型的用逗号分隔的列表</span><br><span class="line">######4). web.xml文件</span><br></pre></td></tr></table></figure></p>
<p>&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;</p>
<p><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><br>  <display-name>文件上传</display-name><br>  <welcome-file-list><br>    <welcome-file>index.html</welcome-file><br>    <welcome-file>index.htm</welcome-file><br>    <welcome-file>/jsp/fileupload.jsp</welcome-file><br>    <welcome-file>default.html</welcome-file><br>    <welcome-file>default.htm</welcome-file><br>    <welcome-file>default.jsp</welcome-file><br>  </welcome-file-list><br>  <filter><br>      <filter-name>struts2</filter-name><br>      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><br>  </filter></web-app></p>
<p>  <filter-mapping><br>      <filter-name>struts2</filter-name><br>      <url-pattern>/*</url-pattern><br>  </filter-mapping><br><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">######5). 执行测试</span><br><span class="line">![图11.png](https://upload-images.jianshu.io/upload_images/3110861-262dd82ed3865902.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</span><br><span class="line">点击upload后</span><br><span class="line">![图12.png](https://upload-images.jianshu.io/upload_images/3110861-144948d644b92b1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</span><br><span class="line"></span><br><span class="line">####6. 全局异常映射</span><br><span class="line">struts.xml文件配置</span><br></pre></td></tr></table></figure></p>
<p>&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;<br>&lt;!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”<br>     “<a href="http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;" target="_blank" rel="noopener">http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</a><br><!-- 根标记元素 --></p>
<p><struts><br>    <!-- 开发模式-默认为false --><br>    <constant name="struts.devMode" value="true"><br>    <!-- 可接受的上传文件的最大值（以字节为单位），默认值为250M --><br>    <constant name="struts.multipart.maxSize" value="1000000"><br>    <!-- 声明不同的包, 标签允许配置的分离和模块化 --><br>    <package name="helloworld" extends="struts-default"></package></constant></constant></struts></p>
<pre><code>    &lt;!-- interceptors  --&gt;
    &lt;interceptors&gt;
        &lt;interceptor name=&quot;myInterceptor&quot; class=&quot;com.mazaiting.struct2.MyInterceptor&quot;/&gt;
    &lt;/interceptors&gt;

    &lt;!-- global-exception-mappings  --&gt;
    &lt;global-exception-mappings&gt;
        &lt;exception-mapping result=&quot;error&quot; exception=&quot;java.lang.NullPointerException&quot;/&gt;
    &lt;/global-exception-mappings&gt;

    &lt;!-- action* --&gt;
    &lt;action name=&quot;index&quot;&gt;
        &lt;result&gt;/jsp/index.jsp&lt;/result&gt;
    &lt;/action&gt;
    &lt;action name=&quot;hello&quot;
        class=&quot;com.mazaiting.struct2.HelloWorldAction&quot;
        method=&quot;execute&quot;&gt;
        &lt;!-- 设置action上的请求参数 --&gt;
        &lt;interceptor-ref name=&quot;params&quot;/&gt;
        &lt;!-- 以action执行时间的形式提供简单的分析信息  --&gt;
        &lt;interceptor-ref name=&quot;timer&quot;/&gt;
        &lt;!-- 自定义拦截器 --&gt;
        &lt;interceptor-ref name=&quot;myInterceptor&quot;/&gt;
        &lt;result name=&quot;success&quot;&gt;/jsp/HelloWorld.jsp&lt;/result&gt;
    &lt;/action&gt;
    &lt;!-- more actions can be listed here --&gt;
    &lt;action name=&quot;upload&quot; class=&quot;com.mazaiting.struct2.FileUploadAction&quot;&gt;
        &lt;!-- 注意过滤器的顺序 --&gt;
        &lt;interceptor-ref name=&quot;fileUpload&quot;&gt;
            &lt;param name=&quot;maximumSize&quot;&gt;500000000&lt;/param&gt;
            &lt;param name=&quot;allowedTypes&quot;&gt;image/jpeg,image/gif,text/plain,application/vnd.ms-powerpoint&lt;/param&gt;
            &lt;param name=&quot;allowedExtensions&quot;&gt;.txt,.ppt,.jpeg,.jpg,.gif&lt;/param&gt;
        &lt;/interceptor-ref&gt;
        &lt;interceptor-ref name=&quot;basicStack&quot;/&gt;
        &lt;result name=&quot;success&quot;&gt;/jsp/success.jsp&lt;/result&gt;
        &lt;result name=&quot;error&quot;&gt;/jsp/error.jsp&lt;/result&gt;
    &lt;/action&gt;
&lt;/package&gt;
&lt;!-- more packages can be listed here --&gt;
</code></pre><p><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">####6. 注解</span><br><span class="line">######1). 添加jar包</span><br><span class="line">在之前导入包的基础上新增antlr-2.7.2.jar、asm.3.3.jar、asm-commons-3.3.jar和struts2-convention-plugin-2.3.34.jar</span><br><span class="line">![图13.png](https://upload-images.jianshu.io/upload_images/3110861-dcabec1ed32a210f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</span><br><span class="line"></span><br><span class="line">######2). 创建视图</span><br><span class="line">I. 创建主页</span><br></pre></td></tr></table></figure></p>
<p>&lt;%@ page language=”java” contentType=”text/html; charset=UTF-8”<br>    pageEncoding=”UTF-8”%&gt;<br>&lt;%@ taglib prefix=”s” uri=”/struts-tags”%&gt;<br>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “<a href="http://www.w3.org/TR/html4/loose.dtd&quot;&gt;" target="_blank" rel="noopener">http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</a></p>
<p><html><br>    <head><br>        <meta http-equiv="pragma" content="no-cache"><br>        <meta http-equiv="cache-control" content="no-cache"><br>        <meta http-equiv="expires" content="0"><br>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br>        <meta http-equiv="description" content="This is my page"><br>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>        <title>Employee Form</title><br>    </head><br>    <body><br>         &lt;s:form action=”empinfo” method=”post”&gt;<br>             &lt;s:textfield name=”name” label=”Name” size=”20”/&gt;<br>             &lt;s:textfield name=”age” label=”Age” size=”20”/&gt;<br>             &lt;s:submit name=”submit” label=”Submit” align=”center”/&gt;<br>        &lt;/s:form&gt;<br>    </body><br></html><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">II. 创建成功视图</span><br></pre></td></tr></table></figure></p>
<p>&lt;%@ page language=”java” contentType=”text/html; charset=UTF-8”<br>    pageEncoding=”UTF-8”%&gt;<br>&lt;%@ taglib prefix=”s” uri=”/struts-tags”%&gt;<br>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “<a href="http://www.w3.org/TR/html4/loose.dtd&quot;&gt;" target="_blank" rel="noopener">http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</a></p>
<p><html><br>    <head><br>        <meta http-equiv="pragma" content="no-cache"><br>        <meta http-equiv="cache-control" content="no-cache"><br>        <meta http-equiv="expires" content="0"><br>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br>        <meta http-equiv="description" content="This is my page"><br>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>        <title>Success</title><br>    </head><br>    <body><br>        Employee Information is captured successfully.<br>    </body><br></html><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">III. 创建失败视图</span><br></pre></td></tr></table></figure></p>
<p>&lt;%@ page language=”java” contentType=”text/html; charset=UTF-8”<br>    pageEncoding=”UTF-8”%&gt;<br>&lt;%@ taglib prefix=”s” uri=”/struts-tags”%&gt;<br>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “<a href="http://www.w3.org/TR/html4/loose.dtd&quot;&gt;" target="_blank" rel="noopener">http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</a></p>
<p><html><br>    <head><br>        <meta http-equiv="pragma" content="no-cache"><br>        <meta http-equiv="cache-control" content="no-cache"><br>        <meta http-equiv="expires" content="0"><br>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br>        <meta http-equiv="description" content="This is my page"><br>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>        <title>Error</title><br>    </head><br>    <body><br>        Employee Information is captured error.<br>    </body><br></html><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">######3). 创建Action</span><br></pre></td></tr></table></figure></p>
<p>@Results({<br>    @Result(name=”success”, location=”/jsp/success.jsp”),<br>    @Result(name=”error”, location=”/jsp/error.jsp”),<br>    @Result(name=”input”, location=”/jsp/index.jsp”)<br>})<br>public class EmployeeAction extends ActionSupport{<br>    private String name;<br>    private int age;</p>
<pre><code>@Action(value=&quot;/empinfo&quot;)
@Override
public String execute() throws Exception {
    return SUCCESS;
}

@RequiredFieldValidator(message=&quot;The name is required.&quot;)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@IntRangeFieldValidator(message=&quot;Age must be in between 28 and 65&quot;,
        min = &quot;29&quot;, max = &quot;65&quot;)
public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
</code></pre><p>}<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">######4). 配置文件web.xml</span><br></pre></td></tr></table></figure></p>
<p>&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;</p>
<p><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><br>  <display-name>StrutsAnnoTest</display-name><br>  <welcome-file-list><br>    <welcome-file>index.html</welcome-file><br>    <welcome-file>index.htm</welcome-file><br>    <welcome-file>/jsp/index.jsp</welcome-file><br>    <welcome-file>default.html</welcome-file><br>    <welcome-file>default.htm</welcome-file><br>    <welcome-file>default.jsp</welcome-file><br>  </welcome-file-list><br>  <filter><br>      <filter-name>struts2</filter-name><br>      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><br>      <init-param><br>          <param-name>struts.devMode</param-name><br>          <param-value>true</param-value><br>      </init-param><br>  </filter><br>  <filter-mapping><br>      <filter-name>struts2</filter-name><br>      <url-pattern>/*</url-pattern><br>  </filter-mapping><br></web-app><br><code>`</code></p>
<p>######5. 测试<br><img src="https://upload-images.jianshu.io/upload_images/3110861-7a909b5d3ec830b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图14.png"></p>
<p>######6. 项目结构图<br><img src="https://upload-images.jianshu.io/upload_images/3110861-0a8bcb1bf76bfa33.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图15.png"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(二)/" rel="next" title="JakartaEE-Spring、MyBatis和SpringMVC的整合(二)">
                <i class="fa fa-chevron-left"></i> JakartaEE-Spring、MyBatis和SpringMVC的整合(二)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JavaEE-Hibernate使用/" rel="prev" title="JavaEE-Hibernate使用">
                JavaEE-Hibernate使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
